package com.onepunch.service;

/**
 * @Author: 代码的路
 * @Date: 2025/10/30 05:38
 * @Param:
 * @Return:
 * @Description:
 **/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.onepunch.common.DateUtils;
import com.onepunch.common.PageResult;
import com.onepunch.entity.Strategy;
import com.onepunch.mapper.StrategyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @projectName: onePunch
 * @package: com.onepunch.service
 * @className: StrategyService
 * @author: yx
 * @description: TODO
 * @date: 2025/10/30 05:38
 * @version: 1.0
 */
@Service
public class StrategyServiceImpl implements StrategyService {

    @Autowired
    private StrategyMapper strategyMapper;

    public int addStrategy(Strategy strategy){
        // 生成当前时间的字符串格式（如 "2024-10-30 16:00:00"）
        strategy.setCreateTime(LocalDateTime.now());

        // 若发布时间为空，默认设为当前时间
        if (strategy.getPublishTime() == null) {
            strategy.setPublishTime(LocalDateTime.now());
        }
        int i = strategyMapper.addStrategy(strategy);
        return i;
    }

    @Override
    public PageResult findStrategy(Strategy strategy, int pageNum, int pageSize) {
        // 1. 设置分页参数（PageHelper会自动拦截后续的SQL，添加分页条件）
        PageHelper.startPage(pageNum,pageSize);
        // 2. 执行查询
        List<Strategy> strategyList = strategyMapper.findStrategy(strategy);

        // 3. 封装分页结果（包含总条数、总页数等信息）
        return new PageResult(new PageInfo<>(strategyList));
    }

    @Override
    public Strategy findStrategyById(int id) {
        return strategyMapper.findStrategyById(id);
    }

    @Override
    public int updateStrategy(Strategy strategy) {
        return strategyMapper.updateStrategy(strategy);
    }

    @Override
    public int deleteStrategyById(int id) {
        return strategyMapper.deleteStrategyById(id);
    }
}
